//
//  CRMessageMaker.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/9/28.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CRMessageMaker.h"
#import "CRHeader.h"
#import "CRImageObject.h"
#import "CRAudioObject.h"
#import "CRVideoObject.h"
#import "CRMessageSetting.h"

@implementation CRMessageMaker

+ (CRMessage*)msgWithText:(NSString*)text
{
    CRMessage *textMessage = [[CRMessage alloc] init];
    textMessage.text        = text;
    [self setupMessage:textMessage];
    return textMessage;
}

+ (CRMessage*)msgWithAudio:(NSString*)filePath
{
    CRAudioObject *audioObject = [[CRAudioObject alloc] initWithSourcePath:filePath scene:CRNOSSceneTypeMessage];
    CRMessage *message = [[CRMessage alloc] init];
    message.messageObject = audioObject;
    message.text = @"发来了一段语音";
    [self setupMessage:message];
    return message;
}

+ (CRMessage*)msgWithVideo:(NSString*)filePath
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    CRVideoObject *videoObject = [[CRVideoObject alloc] initWithSourcePath:filePath scene:CRNOSSceneTypeMessage];
    videoObject.displayName = [NSString stringWithFormat:@"视频发送于%@",dateString];
    CRMessage *message = [[CRMessage alloc] init];
    message.messageObject = videoObject;
    message.apnsContent = @"发来了一段视频";
    [self setupMessage:message];
    return message;
}

+ (CRMessage*)msgWithImage:(UIImage*)image
{
    CRImageObject *imageObject = [[CRImageObject alloc] initWithImage:image scene:CRNOSSceneTypeMessage];
    CRImageOption *option  = [[CRImageOption alloc] init];
    option.compressQuality  = 0.7;
    imageObject.option      = option;
    return [CRMessageMaker generateImageMessage:imageObject];
}

+ (CRMessage *)msgWithImagePath:(NSString*)path
{
    CRImageObject * imageObject = [[CRImageObject alloc] initWithFilepath:path scene:CRNOSSceneTypeMessage];
    return [CRMessageMaker generateImageMessage:imageObject];
}

+ (CRMessage *)msgWithImageData:(NSData *)data extension:(NSString *)extension
{
    CRImageObject *imageObject = [[CRImageObject alloc] initWithData:data extension:extension];
    return [CRMessageMaker generateImageMessage:imageObject];
}

+ (CRMessage *)generateImageMessage:(CRImageObject *)imageObject
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    imageObject.displayName = [NSString stringWithFormat:@"图片发送于%@",dateString];
    CRMessage *message     = [[CRMessage alloc] init];
    message.messageObject   = imageObject;
    message.apnsContent = @"发来了一张图片";
    [self setupMessage:message];
    return message;
}

+ (void)setupMessage:(CRMessage *)message
{
    message.apnsPayload = @{
        @"apns-collapse-id": message.messageId,
    };
    
    CRMessageSetting *setting = [[CRMessageSetting alloc] init];
    setting.scene = CRNOSSceneTypeMessage;
    message.setting = setting;
}


@end
